package com.treasure.mall.zbqmallmanager.controller.integral;

import com.treasure.mall.biz.dto.condition.integral.IntegralOrderCondition;
import com.treasure.mall.biz.entity.integral.IntegralOrderPO;
import com.treasure.mall.biz.service.core.integral.IntegralOrderService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.constant.Constant;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/8/26
 */
@Controller
@RequestMapping("/integral/order")
public class IntegralOrderController {
    @Resource
    private IntegralOrderService orderService;

    @GetMapping("/list")
    public String list(
            @ModelAttribute("condition") IntegralOrderCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<IntegralOrderPO> pos = orderService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);

        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", pos.getTotalElements());
        model.addAttribute("totalPages", pos.getTotalPages());
        model.addAttribute("list", pos.getContent());

        return "integral/order_list";
    }

    @GetMapping("/detail")
    public String detail(String orderId, Model model) {
        IntegralOrderPO po = orderService.findById(orderId);

        model.addAttribute("po", po);

        return "integral/order_detail";
    }

    @PostMapping("/deliver")
    @ResponseBody
    public ApiResult deliver(String orderId, String expressNo, String expressCompany) {
        orderService.deliver(orderId, expressNo, expressCompany);

        return ApiResult.ok();
    }
}
